package server_commands;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import connection_modul.DatabaseConnection;
import server.ServerSocketConnection;

public class BLOCK_LISTCommand extends Command {

	
	public BLOCK_LISTCommand(String parameter) {
		setCode(BLOCK_LIST_CMD);
		setParameter(parameter);
	}
	
	
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {

		String blockListQuery = "SELECT tblBlocks.user_dest " +
								"FROM tblBlocks " +
								"WHERE tblBlocks.user_source='" + getParameter() + "'";
		
		try {
			dbConnection.doStatement(blockListQuery);
		
			ArrayList<Vector> blockList = new ArrayList<Vector>(dbConnection.data);
		
			connection.setMessage( "130 Blocks " + blockList.size() );
		
			for (int i = 0; i < blockList.size(); i++) {
				StringBuffer entry = new StringBuffer();
			
				for (int j = 0; j < blockList.get(i).size(); j++) {
					entry.append(blockList.get(i).get(j));
					if (j < blockList.get(i).size() - 1) {
						entry.append(" ");
					}
				}			
			connection.setMessage( entry.toString() );
			
			dbConnection.clearDataStore();
			}
		} catch (SQLException e) {			
			e.printStackTrace();
			dbConnection.doRollBack();
			return false;
		}
		
		dbConnection.doCommit();
		
		return true;
	}
	
}
